<?php
class ContentForm extends CI_model {
	
	protected $name;
	protected $fieldInfo;
	protected $form;
	protected $formElements;
	protected $formData = array();
	
	public function init( $fieldInfo, $name )
	{
		$this->name = $name;
		$this->fieldInfo = $fieldInfo;
	}
	
	public function process( $edit = false )
	{
		
		$this->load->library('zend','Zend/Form');
		
		//Load the form shiz
		$this->loadFormStuff();
		
		
		$this->load->helper('url');
		if ( !$edit ) {
			$formurl = admin::$adminURL . 'add/' . $this->name . '/';
		} else {
			$formurl = current_url();
		}
		
		$this->form = new Zend_Form;
		$this->form->setAction($formurl)->setMethod('post')->setEnctype(Zend_Form::ENCTYPE_MULTIPART);
		$this->form->setAttrib('id', 'addForm');
		$this->form->setAttrib('class', $this->name . 'addForm');
		
		$this->formElements = array();
		
		foreach($this->fieldInfo as $field) {
			
			if ( $field->Key == 'primary' ) { continue; }
			
			if ( $field->required == true ) { $field->required = false; } else { $field->required = true; }
			
			if ( stripos($field->name,'timestamp') !== false ) {
				$field->Type = 'date';
			}
			
					
			switch ($field->Type) {
				
				case 'text':
				case 'number':
					$this->formElements[$field->name] = new Zend_Form_Element_Text($field->name);
				break;
				case 'longtext':
					$this->formElements[$field->name] = new Zend_Form_Element_TextArea($field->name);
					$this->formElements[$field->name]->setAttrib('class', 'ckeditor');
				break;
				case 'boolean':
					$this->formElements[$field->name] = new Zend_Form_Element_Checkbox($field->name);
				break;
				
				case 'date':
					$this->formElements[$field->name] = new Zend_Form_Element_Date($field->name);
					$this->formElements[$field->name]->setRegisterInArrayValidator(false);
				break;
				
			}
			
			$hidden = false;
			if ( $field->name == 'author' ) {
				$this->formElements[$field->name] = new Zend_Form_Element_Hidden($field->name);
				$field->required = false;
				$hidden = true;
			}
			
			if ( isset($this->formElements[$field->name]) ) {
				
				if ( $field->required ) { $this->formElements[$field->name]->setRequired($field->required); }
				
				if ($hidden == false) {$this->formElements[$field->name]->setLabel(ucfirst(strtolower($field->name)) . ':');}
				
				if ( isset($this->formData[$field->name]) ) { $this->formElements[$field->name]->setValue($this->formData[$field->name]); } 
				if ( isset($field->Type) == 'date' && isset($this->formData[$field->name]) ) {  } 
				$this->form->addElement($this->formElements[$field->name],$field->name);
			}			
			
		}
		
		$this->formElements['Submit'] = new Zend_Form_Element_Submit('Submit');
		$this->formElements['Submit']->setLabel('Submit' . ':');
		$this->formElements['Submit']->setAttribs(array('class' => 'roundBtn redBtn'));
		$this->form->addElement($this->formElements['Submit'],'Submit');
		
	}
	
	public function isValid()
	{
		
		$valid = $this->form->isValid($_POST);
		
		if (!$valid) {
        	$codes = $this->form->getErrors();
        	//$messages =  $this->form->getMessages();
    	}
		
		return $valid;
		
	}
	
	public function draw( $data, $edit = false )
	{
		
		$view = new Zend_View();
		if ( count($data) > 0 ) {
			
			foreach ($data as $k=>$v) {
				$data[$k] = html_entity_decode($v,ENT_QUOTES,'UTF-8');
			}
			
			$this->form->populate($data);
		}
		
		if ( !isset($data['author']) || empty($data['author'])  ) { $data = array(); $data['author'] = admin::$currentUser->id; $this->form->populate($data); }
		
		return $this->form->render($view);

	}
	
	protected function loadFormStuff()
	{
		$this->zend->load('Zend/Form');
		$this->zend->load('Zend/View');
		$this->zend->load('Zend/Form/Element/Text');
		$this->zend->load('Zend/Form/Element/Textarea');
		$this->zend->load('Zend/Form/Element/Checkbox');
		$this->zend->load('Zend/Form/Element/Hidden');
		$this->zend->load('Zend/Form/Element/Button');
		$this->zend->load('Zend/Form/Element/Submit');
		$this->zend->load('Zend/Form/Element/Date');
	}
	
}